from flask import Blueprint, jsonify
from flask_restful import reqparse
from db import db
import time, random


tool_bp = Blueprint('tool_bp', __name__)


# 收货管理
@tool_bp.route('/tool', methods=['GET', 'POST'])
def tool():
    req = reqparse.RequestParser()
    req.add_argument('dsite', required=True)      # '发货地址',
    req.add_argument('tsite', required=True)      # '目的地',
    req.add_argument('dname', required=True)      # '托运人（发货人）',
    req.add_argument('dmobile', required=True)    # '托运人电话',
    snumber = str(int(time.time())) + str(random.randint(10000, 99999))    # '订单编号',
    req.add_argument('money', required=True)      # '运费',
    args = req.parse_args()
    print(args['dsite'])
    print(args['tsite'])
    print(args['dname'])
    print(args['dmobile'])
    print(args['money'])

    # 添加信息
    sql = f"insert into toll_admin(dsite,tsite,dname,dmobile,snumber,money) values(" \
        f"'{args['dsite']}'," \
        f"'{args['tsite']}'," \
        f"'{args['dname']}'," \
        f"'{args['dmobile']}'," \
        f"'{snumber}'," \
        f"{float(args['money'])})"
    db.update(sql)
    db.commit()

    return jsonify({'code': 200, 'msg': '添加成功'})


